<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 菜单
 * Class Menu
 * @package app\api\controller
 */
class Menu extends Api
{
	protected $noNeedLogin = [];
	protected $noNeedRight = '*';

	/**
	 * 列
	 */
	public function lists()
	{
		$type = $this->request->post('type', 1);
		$isDis = $this->request->post("isDis", 1);
		if (!$type) {
			$this->error(__('Invalid parameters'));
		}
		$uid = $this->auth->id;
		$data = [];
		$dao = new \app\common\model\User;
		$user = $dao->where("id", $uid)->find();
		if ($user) {
			$where["pid"] = 1;
			if ($type > 1)  $where["pid"] = 3;
			$disWhere = "";
			// 按角色组显示前端动态菜单
			if ($isDis == 1) $disWhere = " FIND_IN_SET(" . $user->group_id . ",depart) and is_display != 4";
			// 按显示类型显示前端动态菜单
			if ($isDis == 4) $disWhere = 'is_display = ' . $isDis;
			$ret = Db::name("app_menu")
				->where("status", 1)
				->where($where)
				->where($disWhere)
				->field("id,type,name,path,logo_image imgUrl")
				->order("sorts asc")
				->select();
			if ($ret) {
				foreach ($ret as $k => $v) {
					$temp = [];
					$check = $this->display_auths($v['id'], $uid);
					if ($check) {
						$temp = $v;
						array_push($data, $temp);
					}
				}
			}
		}
		$this->success("动态菜单", $data);
	}

	/**
	 * 显示权限
	 */
	private function  display_auths($id, $uid)
	{
		$arr = [
			7,  // 免费申领 - 按钮
		];
		if (!in_array($id, $arr)) return true;
		if (in_array($id, $arr)) {
			$find =	Db::name("apply_access")->field("is_apply apply")->where("uid", $uid)->where("status", 1)->find();
			if ($find) {
				if ($find["apply"] == 1) {
					return true;
				}
			}
			return false;
		}
	}
}
